﻿using UnityEngine;
using System.Collections;

public class CameraMovment : MonoBehaviour {
	public static CameraMovment Instance;
	public GameObject mainCharacter;
	[Range(0.5f,1.5f)]
	public float movingSpeed;
	[Range(2,5)]
	public float panningCountDownTime;
	public CameraState cameraState{get;private set;}

	private string _mainCharacterName;
	private MainCharacterController _mainCtrl;
	private Vector3 _zOffset;
	private Vector3 _previousPos, _currentPos, _xDelta, _yDelta;
	private bool _mouseDown, _isCameraPanning;
	void Awake(){
		Instance = this;
		cameraState = CameraState.Hover;
		_zOffset = new Vector3(0, 0, -10.0f);
	}
	// Use this for initialization
	void Start () {
		_mainCharacterName = "MainCharacter";
		_mainCtrl = GameObject.Find(_mainCharacterName).GetComponent<MainCharacterController>();

		_xDelta = new Vector3(movingSpeed, 0.0f, 0.0f);
		_yDelta = new Vector3(0.0f, movingSpeed, 0.0f);
	}
	
	// Update is called once per frame
	void Update () {
		if(_mainCtrl != null && GlobalInfo.gameState != GameStates.Pause && cameraState == CameraState.Hover){
			 transform.position = _mainCtrl.gameObject.transform.position + _zOffset;
		}

		else if(GlobalInfo.gameState == GameStates.Pause){
			cameraState = CameraState.Panning;
			#if UNITY_STANDALONE || UNITY_WEBPLAYER || UNITY_EDITOR
			if(Input.GetMouseButtonDown(0)){
				_mouseDown = true;
				_previousPos = Input.mousePosition;
			}else if(Input.GetMouseButtonUp(0)){
				_mouseDown = false;
			}
			if(_mouseDown){
				MouseToPan();
			}
			#endif
			#if UNITY_IPONE || UNITY_ANDROID
			if(Input.GetTouch(0).phase == TouchPhase.Began){
				_mouseDown = true;
				_previousPos = Input.GetTouch(0).position;
			}else if(Input.GetTouch(0).phase == TouchPhase.Ended){
				_mouseDown = false;
			}
			if(_mouseDown){
				TouchToPan();
			}
			#endif
		}
	}
	
	public void SetCameraToRespawnPoint(float xPos, float yPos){
		transform.position = new Vector3(xPos - 3, yPos + 3, transform.position.z);
	}
	
	public void SetMainCtrl(MainCharacterController mainController){
		_mainCtrl = mainController;
	}

	public void MoveCameraBack(){
		StartCoroutine(panningBack());
	}

	IEnumerator panningBack(){
		Vector3 targetPos = _mainCtrl.gameObject.transform.position + _zOffset;
		Vector3 startPos = transform.position;
		float startTime = Time.time;
		float currentTime = Time.time;
		float distanceOffset = 1.0f;
		while(Vector3.Distance(transform.position, targetPos) > distanceOffset){
			transform.position = Vector3.Lerp(startPos, targetPos, (currentTime - startTime) / panningCountDownTime);
			currentTime += Time.deltaTime;
			yield return new WaitForSeconds(Time.deltaTime);
		}
		yield return new WaitForSeconds(0.5f);
		cameraState = CameraState.Hover;
		yield return null;
	}

	void MouseToPan(){
		_currentPos = Input.mousePosition;
		ToPan();
		_previousPos = _currentPos;
	}
	
	void TouchToPan(){
		_currentPos = Input.GetTouch(0).position;
		ToPan();
		_previousPos = _currentPos;
	}

	void ToPan(){
		float xDiff = _currentPos.x - _previousPos.x;
		HPan(xDiff);
		
		float yDiff = _currentPos.y - _previousPos.y;
		VPan (yDiff);
	}
	
	void HPan(float delta){
		transform.position -= _xDelta * Time.deltaTime * delta;
	}
	
	void VPan(float delta){
		transform.position -= _yDelta * Time.deltaTime * delta;
	}

}
